<?php
/*
 * @FileForDetails: 专业控制器
 * @Date: 2022-11-29 10:41:11
 */
namespace app\crm\controller\product;

use app\common\controller\Backend;
use app\crm\model\product\Major as ProductMajor;
use app\crm\validate\product\Major as ValidateProductMajor;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Major extends Backend
{
    protected $model = null;
    public function initialize() {
        $this->model = new ProductMajor();
    }

    /**
     * @Descripttion: 增加
     * @Date: 2022-11-30 15:53:49
     */
    public function addData()
    {
        // 1.验证请求方式
        if ($this->request->isAjax()) {
            // 2.获取请求数据
            $params = $this->request->post();

            // 3.验证数据
            $validata = new ValidateProductMajor();
            if (!$validata->check($params)) 
            return json(['code' => 0, 'msg' => $this->error($validata->getError())]);

            $result = false;
            Db::startTrans();
            try {
                // 4.数据库操作
                foreach(ProductMajor::all() as $val) {
                    if ($val['name'] == $params['name']) return json(['code' => 0, 'msg' => '该名称已存在']);
                }
                $shopsModel = new ProductMajor();
                $result = $shopsModel->allowField(true)->save($params);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            return $result ? json(['code' => 1, 'msg' => '增加成功']) : json(['code' => 0, 'msg' => '增加失败']);
        }
    }

    /**
     * @Descripttion: 修改
     * @Date: 2022-11-30 15:53:49
     */
    public function updateData()
    {
        // 1.验证请求方式
        if ($this->request->isAjax()) {
            // 2.获取请求数据
            $params = $this->request->post();
            
            // 3.验证数据
            $validata = new ValidateProductMajor();
            if (!$validata->check($params)) 
            return json(['code' => 0, 'msg' => $this->error($validata->getError())]);

            $result = false;
            Db::startTrans();
            try {
                // 4.数据库操作
                $shopsModel = new ProductMajor();
                $result = $shopsModel->allowField(true)->save($params, ['id' => $params['id']]);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            return $result ? json(['code' => 1, 'msg' => '修改成功']) : json(['code' => 0, 'msg' => '修改失败']);
        }
    }

    /**
     * @Descripttion: 渲染专业
     * @Date: 2022-11-30 15:53:49
     */    
    public function index()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->get();
            if (count($data) >= 3) {
                // WhereLike 模糊查询
                $appoint = ProductMajor::WhereLike('name', '%' . $data['name'] . '%')->all();
                return json(['code' => 0, 'msg' => 'success', 'count' => count($appoint), 'data' => $appoint]);
            } else {
                $curr  = $_GET['page'] ?? 1;
                $limit = $_GET['limit'] ?? 5;
                $start = ($curr - 1) * $limit;
                $list  = ProductMajor::limit($start, $limit)->order('id','desc')->select();
                return json(['code' => 0, 'msg' => 'success', 'count' => ProductMajor::count(), 'data' => $list]);
            }
        }
        return $this->fetch();
    }
}